﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace PararUnThread
{
   public static class Program

    {
       /*
        In this case, the thread is initialized with a lambda expression (which in turn is just a short-
        hand version of a delegate). The thread keeps running until stopped becomes true. After that, 
        the t.Join method causes the console application to wait till the thread finishes execution.*/

        public static void ThreadMethod(object o)

        {

            for (int i = 0; i < (int)o; i++)

            {

                Console.WriteLine("ThreadProc: {0}", i);

                Thread.Sleep(0);

            }

        }

        public static void Main()

        {

            bool stopped = false;
            Thread t = new Thread(new ThreadStart(() => 

            {                

                while (!stopped)

                {

                    Console.WriteLine("Running...");

                    Thread.Sleep(1000);

                }

            }));

            

            t.Start();

            Console.WriteLine("Press any key to exit");

            Console.ReadKey();

            stopped  = true;

            t.Join();

        }

    }
}
